image: node:latest

variables:
  AWS_REGION: us-east-1

stages:
  - test
  - deploy_function
  - test_deployed_function
  - deploy_pages

test:
  stage: test
  script:
    - npm install
    - npm test

production:
  stage: deploy_function
  before_script:
    - FAILURE_MESSAGE="Must define \$AWS_ACCESS_KEY_ID and \$AWS_SECRET_ACCESS_KEY. Add keys to $CI_PROJECT_URL/-/settings/ci_cd"
    - test -z "$AWS_SECRET_ACCESS_KEY" && echo $FAILURE_MESSAGE && exit 1
    - test -z "$AWS_ACCESS_KEY_ID" && echo $FAILURE_MESSAGE && exit 1
  script:
    - npm install
    - npm run deploy -- --stage production --verbose
  environment: production
  only: 
    - master
  artifacts:
    paths:
      - stack.json
    expire_in: 2 weeks

postdeploy_test:
  stage: test_deployed_function
  only:
    - master
  script:
    - npm install
    - STACK_JSON_FILE=./stack.json npm test featureTests

pages:
  stage: deploy_pages
  script:
    - cp stack.json ./public/stack.json
  environment: production_pages
  only:
    - master
  artifacts:
    paths:
      - ./public/
